// pages/errors_detail/errors_detail.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    errorset:[],
    start:0,
    offset:5,
    bookId:1,
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      title: '加载中',
    })
    let bookId = options.bookId
    this.setData({bookId:bookId})
    let app = getApp()
    wx.request({
      url: app.globalData.url + '/question/error',
      data:{
        openId: app.globalData.userInfo.openid ,
        bookId:bookId,
        start:this.data.start,
        offset:this.data.offset,
      },
      header: {
        'content-type': 'application/json' 
      },
      success: result => {
        let errorset = result.data.data
        
        this.setData({
          errorset:errorset,
          start:this.data.start + this.data.offset,
        })
      }
    })
    wx.hideLoading()
  },
  onReady: function () {

  },
  goToDetail:function(event){
    let index = event.currentTarget.dataset.index
    let app = getApp()
    app.globalData.errorSource = this.data.errorset[index]
    wx.navigateTo({
      url: '/pages/collection/collection?source=error&bookId=' + this.data.bookId + "&index=" + index,
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let app = getApp()
    let delerror = app.globalData.delerror
    let errorset = this.data.errorset
    if(delerror != null && !delerror.hasmoved){
      errorset.splice(delerror.index, 1)
      delerror.hasmoved = true
      app.globalData.delerror = delerror
      this.setData({errorset:errorset})
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let app = getApp()
    wx.showNavigationBarLoading()
    wx.request({
      url: app.globalData.url + '/question/error',
      data:{
        openId: app.globalData.userInfo.openid ,
        bookId: this.data.bookId,
        start: this.data.start,
        offset: this.data.offset,
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: result => {
        let newErrorset = result.data.data
        
        let errorset = this.data.errorset
        for(let i = 0; i < newErrorset.length; ++i){
          errorset.push(newErrorset[i])
        }
        this.setData({
          errorset:errorset,
          start:this.data.start + this.data.offset,
        })
        wx.hideNavigationBarLoading()
      }
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})